3 * Version of FileJournal that logs to a DB table.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileJournal
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IDatabase
;
26 use Wikimedia\Rdbms\DBError
;
29 * Version of FileJournal that logs to a DB table
32 class DBFileJournal
extends FileJournal
{
36 protected $wiki = false; // string; wiki DB name
39 * Construct a new instance from configuration.
41 * @param array $config Includes:
42 * 'wiki' : wiki name to use for LoadBalancer
44 protected function __construct( array $config ) {
45 parent
::__construct( $config );
47 $this->wiki
= $config['wiki'];
51 * @see FileJournal::logChangeBatch()
52 * @param array $entries
53 * @param string $batchId
56 protected function doLogChangeBatch( array $entries, $batchId ) {
57 $status = StatusValue
::newGood();
60 $dbw = $this->getMasterDB();
61 } catch ( DBError
$e ) {
62 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
67 $now = wfTimestamp( TS_UNIX
);
70 foreach ( $entries as $entry ) {
72 'fj_batch_uuid' => $batchId,
73 'fj_backend' => $this->backend
,
74 'fj_op' => $entry['op'],
75 'fj_path' => $entry['path'],
76 'fj_new_sha1' => $entry['newSha1'],
77 'fj_timestamp' => $dbw->timestamp( $now )
82 $dbw->insert( 'filejournal', $data, __METHOD__
);
83 if ( mt_rand( 0, 99 ) == 0 ) {
84 $this->purgeOldLogs(); // occasionally delete old logs
86 } catch ( DBError
$e ) {
87 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
96 * @see FileJournal::doGetCurrentPosition()
97 * @return bool|mixed The value from the field, or false on failure.
99 protected function doGetCurrentPosition() {
100 $dbw = $this->getMasterDB();
102 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
103 [ 'fj_backend' => $this->backend
],
109 * @see FileJournal::doGetPositionAtTime()
110 * @param int|string $time Timestamp
111 * @return bool|mixed The value from the field, or false on failure.
113 protected function doGetPositionAtTime( $time ) {
114 $dbw = $this->getMasterDB();
116 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
118 return $dbw->selectField( 'filejournal', 'fj_id',
119 [ 'fj_backend' => $this->backend
, "fj_timestamp <= $encTimestamp" ],
121 [ 'ORDER BY' => 'fj_timestamp DESC' ]
126 * @see FileJournal::doGetChangeEntries()
127 * @param int|null $start
131 protected function doGetChangeEntries( $start, $limit ) {
132 $dbw = $this->getMasterDB();
134 $res = $dbw->select( 'filejournal', '*',
136 'fj_backend' => $this->backend
,
137 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
139 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
140 $limit ?
[ 'LIMIT' => $limit ] : [] )
144 foreach ( $res as $row ) {
146 foreach ( (array)$row as $key => $value ) {
147 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
156 * @see FileJournal::purgeOldLogs()
157 * @return StatusValue
160 protected function doPurgeOldLogs() {
161 $status = StatusValue
::newGood();
162 if ( $this->ttlDays
<= 0 ) {
163 return $status; // nothing to do
166 $dbw = $this->getMasterDB();
167 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays
);
169 $dbw->delete( 'filejournal',
170 [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
178 * Get a master connection to the logging DB
183 protected function getMasterDB() {
185 // Get a separate connection in autocommit mode
186 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory()->newMainLB();
187 $this->dbw
= $lb->getConnection( DB_MASTER
, [], $this->wiki
);
188 $this->dbw
->clearFlag( DBO_TRX
);